package com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden;

import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.ServiceFeedbackActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.loan_wooden.LoanMainActivity_wooden;
import com.test.nnooolwdeaer.adapter_wooden.CustomerServiceListAdapter_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.AppShowInfo_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseFragment_wooden;

public class PersonalCenterFragment_wooden extends BaseFragment_wooden {

    private View serviceFeedbackLayout_wooden;
    private ListView customerServiceLv_wooden;
    private TextView customerServiceDescTv_wooden;

    private CustomerServiceListAdapter_wooden customerServiceListAdapter_wooden;
    private BaseActivity_wooden activity_wooden;

    private int listHeight = 0;

    public PersonalCenterFragment_wooden(){}

    public PersonalCenterFragment_wooden(BaseActivity_wooden activity){
        this.activity_wooden = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal_center_wooden, container, false);
        init_wooden(view);
        return view;
    }

    private void init_wooden(View view){

        serviceFeedbackLayout_wooden = view.findViewById(R.id.fragment_personal_center_layout_service_feedback);
        customerServiceLv_wooden = view.findViewById(R.id.fragment_personal_center_lv_customer_service);
        customerServiceDescTv_wooden = view.findViewById(R.id.fragment_personal_center_tv_customer_service_desc);

        String desc = AppShowInfo_wooden.getInstance().getCustomerDesc();
        if (!TextUtils.isEmpty(desc)){
            customerServiceDescTv_wooden.setText(desc);
        }

        serviceFeedbackLayout_wooden.setOnClickListener(v -> {
            if (activity_wooden!=null) activity_wooden.startActivity_wooden(ServiceFeedbackActivity_wooden.class);
        });

        customerServiceListAdapter_wooden = new CustomerServiceListAdapter_wooden(requireContext());
        customerServiceLv_wooden.setAdapter(customerServiceListAdapter_wooden);

        view.findViewById(R.id.fragment_personal_center_layout_setting).setOnClickListener(view1 -> {
            LoanMainActivity_wooden.launchSettingFlag = true;
            HttpManager_wooden.getInstance_wooden().fetchDropConfig();
        });

        updateCustomerService_wooden();

    }

    public void updateCustomerService_wooden(){

        customerServiceListAdapter_wooden.initCustomerService_wooden();
        customerServiceListAdapter_wooden.notifyDataSetChanged();

        ViewGroup.LayoutParams layoutParams = customerServiceLv_wooden.getLayoutParams();
        if (listHeight==0) listHeight = layoutParams.height;
        layoutParams.height = customerServiceListAdapter_wooden.getCount()*listHeight;
        customerServiceLv_wooden.setLayoutParams(layoutParams);
    }

}
